package com.wyd.wenyongdaAdmin.admin.role.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.wyd.wenyongdaAdmin.commons.model.admin.role.dto.RoleDTO;
import com.wyd.wenyongdaAdmin.commons.util.ResultEnum;
import com.wyd.wenyongdaAdmin.commons.util.ResultT;
import com.wyd.wenyongdaAdmin.commons.util.ResultUtil;
import com.wyd.wenyongdaAdmin.xo.admin.role.service.IRoleMenuService;
import com.wyd.wenyongdaAdmin.xo.admin.role.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "角色控制器")
@RestController
@RequestMapping("/role")
public class RoleController {

    private static final Logger log = LoggerFactory.getLogger(RoleController.class);

    private final IRoleService roleService;

    private final IRoleMenuService roleMenuService;

    @Autowired
    RoleController(IRoleService roleService, IRoleMenuService roleMenuService) {
        this.roleService = roleService;
        this.roleMenuService = roleMenuService;
    }

    @GetMapping("/get/all")
    public ResultUtil selectAllRole(@RequestParam Integer isEnabled) {
        List<RoleDTO> roleDTOList = this.roleService.selectAllRole(isEnabled);
        return ResultUtil.ok().data("list", roleDTOList);
    }

    @ApiOperation("分页查询角色")
    @PostMapping("/get/page")
    public ResultUtil selectRolePageWithCount(@RequestBody Map<String, Object> reqParams) {
        RoleDTO roleDTO = null;
        if (ObjectUtil.isNotNull(reqParams.get("name")) || ObjectUtil.isNotNull(reqParams.get("code")) ||
        ObjectUtil.isNotNull(reqParams.get("isEnabled"))) {
            roleDTO = new RoleDTO();
            if (ObjectUtil.isNotNull(reqParams.get("name"))) {
                roleDTO.setName(reqParams.get("name").toString());
            }
            if (ObjectUtil.isNotNull(reqParams.get("code"))) {
                roleDTO.setCode(reqParams.get("code").toString());
            }
            if (StrUtil.isNotBlank(String.valueOf(reqParams.get("isEnabled")))) {
                Integer isEnabled = Integer.valueOf(reqParams.get("isEnabled").toString());
                roleDTO.setIsEnabled(isEnabled);
            }
        }
        Integer pageNumber = (Integer) reqParams.get("pageNumber");
        Integer pageSize = (Integer) reqParams.get("pageSize");
        PageInfo<RoleDTO> pageInfo = this.roleService.selectRolePageWithCount(pageNumber, pageSize, roleDTO);
        return ResultUtil.ok().data("records", pageInfo);
    }

    @PostMapping("/save")
    public ResultUtil addRole(@RequestBody Map<String, Object> reqParams) {
        RoleDTO roleDTO = BeanUtil.fillBeanWithMap(reqParams, new RoleDTO(), false);
        if (this.roleService.addRole(roleDTO) == 0) {
            return ResultUtil.error(ResultEnum.ROLE_ERROR_OR_UNKNOWN);
        } else {
            return ResultUtil.ok();
        }
    }

    @PutMapping("/update")
    public ResultUtil updateRole(@RequestBody Map<String, Object> reqParams) {
        RoleDTO roleDTO = BeanUtil.fillBeanWithMap(reqParams, new RoleDTO(), false);
        if (this.roleService.updateRole(roleDTO) == 0) {
            return ResultUtil.error(ResultEnum.ROLE_ERROR_OR_UNKNOWN);
        } else {
            return ResultUtil.ok();
        }
    }

    @DeleteMapping("/delete")
    public ResultUtil deleteRole(@RequestBody Map<String, Object> reqParams) {
        if (ObjectUtil.isNotNull(reqParams.get("ids"))) {
            List<Long> ids = Convert.toList(Long.TYPE ,reqParams.get("ids"));
            if (this.roleService.deleteRole(ids) == 0) {
                return ResultUtil.error(ResultEnum.ROLE_ERROR_OR_UNKNOWN);
            } else {
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultEnum.UNKNOWN_ERROR);
    }

    @GetMapping("/get/role/menu/ids")
    public ResultT<List<Long>> getRoleMenuIds(@RequestParam Long roleId) {
        List<Long> currentRoleMenuIds = this.roleMenuService.getRoleMenuIdsByRoleId(roleId);
        return ResultT.<List<Long>>ok().data(currentRoleMenuIds);
    }

    @PutMapping("/update/role/menu/ids")
    public ResultUtil updateRoleMenuIds(@RequestBody Map<String, Object> reqParams) {
        if (ObjectUtil.isNotNull(reqParams.get("roleId"))){
            Map<String, Object> params = JSONObject.parseObject(JSONObject.toJSONString(reqParams.get("params")));
            List<Long> menuIds = Convert.toList(Long.TYPE ,params.get("menuIds"));
            Long roleId = Long.valueOf(reqParams.get("roleId").toString());
            if (this.roleMenuService.updateRoleMenuIdsByRoleId(roleId, menuIds) > 0) {
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultEnum.UNKNOWN_ERROR);
    }
}
